<?php
//列表控制器类
class ListController extends CommonController{
    private $model;
    /**
     * [__init 构造函数]
     * @return [type] [description]
     */
    public function __init(){
        parent::__init();
        $this->model=K("Category");
    }
    //动作方法
    public function index(){
    	// 分类栏目数据
    	$cateData = $this->model->get_data();
        $cateD =$cateData;
    	$arr = array();
    	foreach ($cateData as $k => $v) {
    		if ($v['pid']==0) {
    			$arr[$k]= $v;
    			$arr[$k]["son"]=$this->get_son($cateData,$v['cid']);
    		}
    	}
    	$cateData = $arr;
        $this->assign('cateData',$cateData);
        // 分类产品数据获取
        $cid  = Q('get.cid',0,"intval");
        // 判断分类传参数是否符合
        if($cid<1){
            $this->success('没有当前分类',__ROOT__);
        }
        // 获取父级分类下的子分类的
        $sonData=$this->get_son($cateD,$cid);
        // p($sonData);
        $son ="";
        foreach ($sonData as $v) {
            $son .=$v['cid'].",";  
        }
        $son=rtrim($son,",");
        if (empty($sonData)) {
            $son=$cid;
        }
        // p($son);
        // 获取所有分类下的商品ID
        $where ='cid in('.$son.')';
        // p($where);
        $model =K('Goods');
        $gids = $model->get_gids($where,"gid");
        // 组合查询条件
        if (!empty($gids)){
            $where ='gid in('.implode(',', $gids).')';
        }else{
            $where = 'gid in(0)';
        }
        // 获取当前分类下的属性
        $typeData = K('Category')->get_one_data(array('cid'=>$cid));
        $tid = $typeData['tid'];
        $typeAttr = K('TypeAttr')->get_data(array('tid'=>$tid));
        // 获取当前分类属性的数量
        $num = count($typeAttr);
        foreach ($typeAttr as $k => $v) {
            $typeAttr[$k]['tavalue']=explode(',', $v['tavalue']);
            array_unshift($typeAttr[$k]['tavalue'], '全部');
            // p($typeAttr[$k]['tavalue']);
        }
        // p($typeAttr);
        $this->assign('typeAttr',$typeAttr);
        // 组合发生ID,并分配变量
        // 判断是否有传筛选参数
        $s = isset($_GET['s']) ? explode('_', trim($_GET['s'], '_')) : 0;
        // 组合参数组
        $filter = count($s) != $num ? array_fill(0, $num, 0) : $s;
        // 取得已选择筛选的商品属性ID条件
        $goodsId = array();
        // p($filter);
        foreach ($filter as  $k => $v)
        {
            if ($v > 0)
            {   
                $tavalue = $typeAttr[$k]['tavalue'][$v];
                // p($tavalue);
                $gidData = K('GoodsAttr')->get_gids(array('gtvalue'=>$tavalue),'gid');
                // p($gidData);
                if (!empty($gidData)){
                    $where .= 'AND gid in('.implode(',',$gidData).')';
                }else{
                    $where .= 'AND gid in(0)';
                }
            }
        }
        // p($where);
        // 分配参数组
        $this->assign('filter',$filter);
        // 分配产品数据
        $listData = $model->get_data($where);
        $total=count($listData);
        $pageObj= new Page($total,12,3);
        $page = $pageObj->show();
        $this->assign("page",$page);
        $listData = $model->get_data($where,$pageObj->limit(),'gid desc');
        // p($listData);
        $this->assign('listData',$listData);
        //显示视图
        $this->dis("list.html");
    }

    /**
     * [get_son 找子类]
     * @param  [type] $cate [description]
     * @param  [type] $cid  [description]
     * @return [type]       [description]
     */
    public function get_cateson($cate,$cid){
        $arr =array();
        foreach ($cate as  $v) {
            if($v["pid"]==$cid){
                $arr[]=$v["cid"];
                $arr =array_merge($arr,$this->get_son($cate,$v["cid"]));
            }
        }
        return $arr;
        
    }


}
